package com.companyName.sample.rest.restful;

import com.companyName.sample.common.base.CommBaseController;
import com.companyName.sample.common.bean.ResponseData;
import com.companyName.sample.common.constant.ResponseMsg;
import com.companyName.sample.rest.constant.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

public abstract class BaseController  extends CommBaseController {

    private static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    public String getUserAgent(HttpServletRequest httpRequest) {
        return httpRequest.getHeader(Constants.ReqHeader.UserAgent);
    }


    /**
     * 失败的请求返回 -- 返回 ResponseData对象
     */
    public ResponseData<Object> failureResponse(int code, Object... result) {
        return super.baseFailureResponse(code, result);
    }

    /**
     * 成功的请求返回
     */
    public ResponseData<Object> successResponse(Object... result) {
        return super.baseSuccessResponse(result);
    }
    /**
     * 对返回结果进行封装
     */
    public ResponseData<?> wrapResponse(Object obj) {
        return new ResponseData<Object>(ResponseMsg.SUCCESS.getCode(),obj);
    }

    /**
     * 对返回结果进行封装  -- 返回成功状态
     */
    public ResponseData<?> wrapResponseSuccess(Object obj) {
        return new ResponseData<Object>(ResponseMsg.SUCCESS.getCode(),obj);
    }

    /**
     * 对返回结果进行封装  -- 返回错误状态
     */
    public ResponseData<?> wrapResponseFailure(Object obj) {
        return new ResponseData<Object>(ResponseMsg.EXCEPTION.getCode(),obj);
    }

    /**
     * 对返回结果进行封装 -- 抛出异常的处理
     */
    public ResponseData<?> wrapExceptionResponseData(ResponseData<?> responseData,String msg) {
        if(responseData!=null && responseData.getResult() != null){
            return responseData;
        }
        return new ResponseData(ResponseMsg.EXCEPTION.getCode(),null,msg);
    }

}
